// VideoWidget.h
#ifndef VIDEOWIDGET_H
#define VIDEOWIDGET_H

#include "Custom_QGraphicsScene.h"
#include <opencv2/opencv.hpp>
#include <QMessageBox>
#include <QtWidgets>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QTimer>
#include <QThread>
#include <QAudioInput>
#include <QAudioOutput>
#include <QMediaDevices>
#include <QAudioDevice>
#include <QAudiosource>
#include <QBuffer>
#include <QDebug>
#include "Work.h"
#include <QMediaPlayer>
#include <QAudioSink>
#include <QAudio>

class VideoWidget : public QWidget
{
    Q_OBJECT

public:
    explicit VideoWidget(QWidget* parent = nullptr);
    ~VideoWidget();

private slots:
    void captureFrame();
    void updateDisplay(const QImage& image);
    void onOpenMicrophone();
    void onCloseMicrophone();
    void onOpenTelephoneReceiver(QString soundFile);
    void onOpenTelephoneReceiverLocation();
    void onCloseTelephoneReceiver();

private:
    void initializeCamera();
    void initializeOpenPose();
    void initializeAudio();
    bool eventFilter(QObject* obj, QEvent* event) override;

    // 视频相关
    CustomGraphicsView* graphicsView;
    QGraphicsScene* scene;
    cv::VideoCapture capture;
    QTimer* timer;
    int frameCounter;

    // 多线程处理
    QThread* workerThread;
    Worker* worker;

    // OpenPose相关
    cv::dnn::Net openPoseNet;
    bool openPoseLoaded;

    QMediaPlayer* mediaPlayer;
    // 音频相关
    QAudioSource* audioSource;
    QAudioInput* audioInput;
    QAudioOutput* audioOutput;
    QAudioSink* audioOutputLocation;
    QBuffer* audioBuffer;
    bool microphoneOpened;
    bool telephoneReceiverOpened;

    // UI控件
    QPushButton* btnOpenMicrophone;
    QPushButton* btnOpenTelephoneReceiver;
    QPushButton* btnCloseMicrophone;
    QPushButton* btnCloseTelephoneReceiver;

signals:
    void frameToProcess(cv::Mat  frame,bool isSuccess);
};

#endif // VIDEOWIDGET_H